package com.pig4cloud.pig.demo.Service.impl;

import com.pig4cloud.pig.demo.Mapper.ConlikeMapper;
import com.pig4cloud.pig.demo.Service.ConlikeService;
import com.pig4cloud.pig.demo.Service.UserAndLikeService;
import com.pig4cloud.pig.demo.feign.Entity.Conlike;
import com.pig4cloud.pig.demo.feign.Entity.Userandlike;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConlikeServiceImpl implements ConlikeService {
    @Autowired
    private ConlikeMapper conlikeMapper;

    @Autowired
    private UserAndLikeService userAndLikeService;

    @Override
    public Conlike findById(Long id) {
        return conlikeMapper.selectById(id);
    }

    @Override
    public int insert(Conlike conlike) {
        return conlikeMapper.insert(conlike);
    }

    @Override
    public Userandlike changeLikeNums(Long uid, Long cid) {
        Userandlike userandlike = userAndLikeService.findByUidAndCid(uid, cid);

        if (userandlike == null) {

        }

        Integer isLiked = userandlike.getIsLiked();
        Integer num = userandlike.getConlike().getNum();
        //
        if (isLiked == 0) {
            userandlike.setIsLiked(1);
            userandlike.getConlike().setNum(num + 1);
        } else {
            userandlike.setIsLiked(0);
            userandlike.getConlike().setNum(num - 1);
        }

        int i = conlikeMapper.updateById(userandlike.getConlike());
        int j = userAndLikeService.updateByUidAndCid(userandlike);
        if (i > 0 && j > 0)
            return userandlike;
        return null;
    }
}
